package com.ruoyi.cderp.service.impl;

import java.util.List;

import com.ruoyi.cderp.domain.*;
import com.ruoyi.cderp.mapper.*;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.service.ITblReceiveSheetService;

/**
 * 采购收货单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-05
 */
@Service
public class TblReceiveSheetServiceImpl implements ITblReceiveSheetService
{
    // 存储当前登录用户对象
    LoginUser loginUser = null;
    @Autowired
    private TblReceiveSheetMapper tblReceiveSheetMapper;
    @Autowired
    private TblPurchaseOrderMapper tblPurchaseOrderMapper;
    @Autowired
    private TblProductStockMapper tblProductStockMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private MaterialStockMapper materialStockMapper;
    /**
     * 查询采购收货单
     *
     * @param id 采购收货单主键
     * @return 采购收货单
     */
    @Override
    public TblReceiveSheet selectTblReceiveSheetById(String id)
    {
        TblReceiveSheet receiveSheet=tblReceiveSheetMapper.selectTblReceiveSheetById(id);
        for (TblReceiveSheetDetail detail:receiveSheet.getTblReceiveSheetDetailList()){
            int num=detail.getPurchaseType();
            if (num==1){
                Material material=materialMapper.selectMaterialById(detail.getMaterialId());
                detail.setMaterial(material);
            }
        }

        return receiveSheet;
    }

    /**
     * 查询采购收货单列表
     *
     * @param tblReceiveSheet 采购收货单
     * @return 采购收货单
     */
    @Override
    public List<TblReceiveSheet> selectTblReceiveSheetList(TblReceiveSheet tblReceiveSheet)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        List<TblReceiveSheet> list=tblReceiveSheetMapper.selectTblReceiveSheetList(tblReceiveSheet);
        for (TblReceiveSheet receiveSheet:list){
            int num=receiveSheet.getTblReceiveSheetDetailList().get(0).getPurchaseType();
            if (num==1){
                for (TblReceiveSheetDetail detail:receiveSheet.getTblReceiveSheetDetailList()){
                    Material material=materialMapper.selectMaterialById(detail.getMaterialId());
                    detail.setMaterial(material);
                }
            }
        }

        return list;
    }

    /**
     * 新增采购收货单
     *
     * @param tblReceiveSheet 采购收货单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTblReceiveSheet(TblReceiveSheet tblReceiveSheet)
    {
        //采购订单修改
        tblPurchaseOrderMapper.deleteTblPurchaseOrderDetailByOrderId(tblReceiveSheet.getPurchaseOrderId());
        for (TblReceiveSheetDetail tblReceiveSheetDetail : tblReceiveSheet.getTblReceiveSheetDetailList()) {
            List<TblPurchaseOrderDetail> list=new ArrayList<>();
            list.add(tblReceiveSheetDetail.getTblPurchaseOrderDetail());
            tblPurchaseOrderMapper.batchTblPurchaseOrderDetail(list);
        }

        //采购收货单新增
        tblReceiveSheet.setCreateTime(DateUtils.getNowDate());

        tblReceiveSheet.setCreateById(loginUser.getUser().getUserId().toString());
        tblReceiveSheet.setCreateBy(loginUser.getUser().getUserName());

        int rows = tblReceiveSheetMapper.insertTblReceiveSheet(tblReceiveSheet);
        insertTblReceiveSheetDetail(tblReceiveSheet);
        return rows;
    }

    /**
     * 修改采购收货单
     *
     * @param tblReceiveSheet 采购收货单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTblReceiveSheet(TblReceiveSheet tblReceiveSheet)
    {
        //商品库存修改
        if(tblReceiveSheet.getStatus()==3){
            for (TblReceiveSheetDetail tblReceiveSheetDetail : tblReceiveSheet.getTblReceiveSheetDetailList()) {
                if (tblReceiveSheet.getTblReceiveSheetDetailList().get(0).getPurchaseType()==0){
                    tblProductStockMapper.deleteTblProductStockById(tblReceiveSheetDetail.getBaseDataProduct().getTblProductStock().getId());
                    tblProductStockMapper.insertTblProductStock(tblReceiveSheetDetail.getBaseDataProduct().getTblProductStock());
                }else if (tblReceiveSheet.getTblReceiveSheetDetailList().get(0).getPurchaseType()==1){
                    materialStockMapper.deleteMaterialStockById(tblReceiveSheetDetail.getMaterial().getMaterialStock().getId());
                    materialStockMapper.insertMaterialStock(tblReceiveSheetDetail.getMaterial().getMaterialStock());
                }
            }
        }

        //采购订单修改
        tblPurchaseOrderMapper.deleteTblPurchaseOrderDetailByOrderId(tblReceiveSheet.getPurchaseOrderId());
        for (TblReceiveSheetDetail tblReceiveSheetDetail : tblReceiveSheet.getTblReceiveSheetDetailList()) {
            List<TblPurchaseOrderDetail> list=new ArrayList<>();
            list.add(tblReceiveSheetDetail.getTblPurchaseOrderDetail());
            tblPurchaseOrderMapper.batchTblPurchaseOrderDetail(list);
        }

        //采购收货单修改
        tblReceiveSheet.setUpdateTime(DateUtils.getNowDate());

        tblReceiveSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        tblReceiveSheet.setUpdateBy(loginUser.getUser().getUserName());
        if (tblReceiveSheet.getApproveBy()!=null){
            tblReceiveSheet.setApproveBy((loginUser.getUser().getUserName()));
        }

        tblReceiveSheetMapper.deleteTblReceiveSheetDetailBySheetId(tblReceiveSheet.getId());
        insertTblReceiveSheetDetail(tblReceiveSheet);

        return tblReceiveSheetMapper.updateTblReceiveSheet(tblReceiveSheet);
    }

    /**
     * 批量删除采购收货单
     *
     * @param ids 需要删除的采购收货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblReceiveSheetByIds(String[] ids)
    {
        tblReceiveSheetMapper.deleteTblReceiveSheetDetailBySheetIds(ids);
        return tblReceiveSheetMapper.deleteTblReceiveSheetByIds(ids);
    }

    /**
     * 删除采购收货单信息
     *
     * @param id 采购收货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTblReceiveSheetById(String id)
    {
        TblReceiveSheet tblReceiveSheet=selectTblReceiveSheetById(id);

        List<TblPurchaseOrderDetail> list=new ArrayList<>();
        for (TblReceiveSheetDetail tblReceiveSheetDetail : tblReceiveSheet.getTblReceiveSheetDetailList()){
            TblPurchaseOrderDetail detail=tblReceiveSheetDetail.getTblPurchaseOrderDetail();
            detail.setReceiveNum(detail.getReceiveNum()-tblReceiveSheetDetail.getReceiveNum());
            list.add(detail);
        }

        tblPurchaseOrderMapper.deleteTblPurchaseOrderDetailByOrderId(tblReceiveSheet.getPurchaseOrderId());
        tblPurchaseOrderMapper.batchTblPurchaseOrderDetail(list);

        tblReceiveSheetMapper.deleteTblReceiveSheetDetailBySheetId(id);
        return tblReceiveSheetMapper.deleteTblReceiveSheetById(id);
    }

    /**
     * 新增采购收货单明细信息
     *
     * @param tblReceiveSheet 采购收货单对象
     */
    public void insertTblReceiveSheetDetail(TblReceiveSheet tblReceiveSheet)
    {
        List<TblReceiveSheetDetail> tblReceiveSheetDetailList = tblReceiveSheet.getTblReceiveSheetDetailList();
        String id = tblReceiveSheet.getId();
        if (StringUtils.isNotNull(tblReceiveSheetDetailList))
        {
            List<TblReceiveSheetDetail> list = new ArrayList<TblReceiveSheetDetail>();
            for (TblReceiveSheetDetail tblReceiveSheetDetail : tblReceiveSheetDetailList)
            {
                tblReceiveSheetDetail.setSheetId(id);
                list.add(tblReceiveSheetDetail);
            }
            if (list.size() > 0)
            {
                tblReceiveSheetMapper.batchTblReceiveSheetDetail(list);
            }
        }
    }
}
